package com.fanh.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fanh.common.api.CommonPage;
import com.fanh.common.api.CommonResult;
import com.fanh.common.exception.Asserts;
import com.fanh.common.util.CommonUtil;
import com.fanh.common.util.SmsCodeUtil;
import com.fanh.model.GuideCoinOrder;
import com.fanh.model.GuidePay;
import com.fanh.model.SysUser;
import com.fanh.service.GuideCoinOrderService;
import com.fanh.service.GuidePayService;
import com.fanh.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@Api(tags = "PayController", description = "支付管理API")
@RequestMapping("/a")
public class PayController {



    @Autowired
    private GuideCoinOrderService guideCoinOrderService;
    @Autowired
    private GuidePayService guidePayService;
    @Autowired
    private UserService userService;

    //生成订单
    @ApiOperation("订单生成")
    @RequestMapping(value = "/app/order/createOrder", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult createOrder(@ApiParam(value = "用户ID", required = true)@RequestParam(name = "userId") String userId,
                                    @ApiParam(value = "购买数量，默认购买单价为1元", required = true) @RequestParam(name = "number") Integer number,
                                    @ApiParam(value = "1-支付宝，2-微信，3-苹果", required = true) @RequestParam(name = "payType") Integer payType,
                                    @ApiParam( value = "订单来源 0->教师端；1->学生端 ", required = true) @RequestParam(name = "sourceType") Integer sourceType,
                                    @ApiParam( value = "赠送的虚拟币,默认为0") @RequestParam(name = "sendIcoinAmount", required = false,defaultValue = "0" ) BigDecimal sendIcoinAmount) {
         GuideCoinOrder order =new GuideCoinOrder();
         order.setOrderSn(CommonUtil.uuid());
        order.setCreateTime(new Date());
        order.setDeleteStatus(0);
        order.setIcoinAmount(new BigDecimal(number)); //默认等于购买数量
        order.setNumber(number);
        order.setPayAmount(new BigDecimal(number));
        order.setPayType(payType);
        order.setSendIcoinAmount(sendIcoinAmount);
        order.setSourceType(sourceType);
        order.setStatus(0);
        order.setUserId(userId);
         order.setTotalAmount(new BigDecimal(number));

        order = guideCoinOrderService.insert(order);

        return CommonResult.success(order,"下单成功");
    }

    //app调用微信支付

    @ApiOperation("微信订单支付")
    @RequestMapping(value = "/app/order/wxPay", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult wxPay(@ApiParam(value = "订单ID", required = true)Long orderId, HttpServletRequest request) throws Exception {
        Map map = guideCoinOrderService.wxPay(orderId,request);
        return CommonResult.success(map);
    }
    //微信回调
    @ApiOperation("微信支付回调")
    @RequestMapping(value = "/app/order/payBack", method = RequestMethod.POST)
     public void payBack(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String resultXML = guideCoinOrderService.payBack(request);
        response.getWriter().write(resultXML);
    }
    //接收客户端返回苹果支付凭证   验证苹果支付

    @ApiOperation("验证IOS支付凭证")
    @RequestMapping(value = "/app/order/applePay", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult applePay( @ApiParam(value = "订单编号", required = true) String orderSn,
                                   String certificateCode)  {
         int count = guideCoinOrderService.applePay(orderSn, certificateCode);
        if (count > 0) {
            return CommonResult.success(1,"支付成功");
        } else {
            return CommonResult.failed("支付失败");
        }
    }


    @ApiOperation("消费记录")
    @RequestMapping(value = "/guide/s/pay/list", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<CommonPage<GuidePay>> payList(@RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                  @RequestParam(required = false, defaultValue = "10") Integer pageSize)  {

        List<GuidePay> list = guidePayService.GuidePayList(userService.getCurrentSysUser().getId(),pageNo,pageSize);
        return CommonResult.success(CommonPage.restPage(list));
    }


}
